package openmind.demo.mapUsage;

import openmind.demo.Employee;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wangzhuangzhuang
 * @DESC:
 * @date 2022-01-06 11:06
 */
public class StreamMapDemo {
	public static void main(String[] args) {

		mapObjectUsage();
	}



	// 处理对象数据格式转换
	private static void mapObjectUsage() {
		Employee e1 = new Employee(1, 23, "M", "Rick", "Beethovan");
		Employee e2 = new Employee(2, 13, "F", "Martina", "Hengis");
		Employee e3 = new Employee(3, 43, "M", "Ricky", "Martin");
		Employee e4 = new Employee(4, 26, "M", "Jon", "Lowman");
		Employee e5 = new Employee(5, 19, "F", "Cristine", "Maria");
		Employee e6 = new Employee(6, 15, "M", "David", "Feezor");
		Employee e7 = new Employee(7, 68, "F", "Melissa", "Roy");
		Employee e8 = new Employee(8, 79, "M", "Alex", "Gussin");
		Employee e9 = new Employee(9, 15, "F", "Neetu", "Singh");
		Employee e10 = new Employee(10, 45, "M", "Naveen", "Jain");
		List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);

		// first style
		List<Employee> mappedEmployee01 = employees.stream()
				.map(e -> {
					e.setAge(e.getAge() + 1);
					e.setGender(e.getGender().equals("M") ? "male" : "female");
					return e;
				}).collect(Collectors.toList());
		System.out.println(mappedEmployee01);

		// second style
		List<Employee> mappedEmployee02 = employees.stream()
				.peek(e -> {
					e.setAge(e.getAge() + 1);
					e.setGender(e.getGender().equals("M") ? "male" : "female");
				}).collect(Collectors.toList());
		System.out.println(mappedEmployee02);

		// 由于map的参数e就是返回值，可以用peek函数，peek函数是一种特殊的map函数。
		// 当函数没有返回值或参数就是返回值就可以使用peek函数
	}

	private static void mapCommonUsage() {
		List<String> animalNames = Arrays.asList("Monkey", "Lion", "Giraffe", "Lemur");

		// 不使用Stream管道流
		List<String> alphaAnimalNames = new ArrayList<String>();
		for (String s : animalNames) {
			alphaAnimalNames.add(s.toUpperCase());
		}
		System.out.println(alphaAnimalNames); // [MONKEY, LION, GIRAFFE, LEMUR]

		// 使用Stream管道流
		List<String> mappedAnimalNames = alphaAnimalNames.stream().map(String::toUpperCase).collect(Collectors.toList());
		System.out.println(mappedAnimalNames); // [MONKEY, LION, GIRAFFE, LEMUR]

		List<Integer> collect = alphaAnimalNames.stream()
				.map(String::length)
				.collect(Collectors.toList());
		System.out.println(collect); // [6, 4, 7, 5]

		Stream.of("Monkey", "Lion", "Giraffe", "Lemur")
				.mapToInt(String::length)
				.forEach(System.out::println);
	}
}